package it.cqupt.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import it.cqupt.dao.system.RoleDao;
import it.cqupt.domain.system.Role;
import it.cqupt.factory.MapperFactory;
import it.cqupt.service.system.RoleService;
import it.cqupt.utils.MD5Util;
import it.cqupt.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class RoleServiceImpl implements RoleService {
    SqlSession sqlSession = null;
    @Override
    public void save(Role role) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            String id = UUID.randomUUID().toString();
            role.setId(id);
            role.setCreateTime(new Date());
            mapper.save(role);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(Role role) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            mapper.delete(role);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Role role) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            mapper.update(role);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public Role findById(String id) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            return mapper.findById(id);
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Role> findAll() {
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            return mapper.findAll();
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            PageHelper.startPage(page,size);
            List<Role> list = mapper.findAll();
            PageInfo pageInfo = new PageInfo(list);
            return pageInfo;
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void updateRoleModule(String roleId, String moduleIds) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            mapper.deleteRoleModule(roleId);
            String[] moduleArray = moduleIds.split(",");
            for(int i = 0;i < moduleArray.length;i++){
                String moduleId = moduleArray[i];
                mapper.saveRoleModule(roleId,moduleId);
            }
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Role> findAllUserRole(String userId) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            return mapper.findAllUserRole(userId);
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
